<?php
namespace app\api\controller;
use think\Db;
use app\admin\model\Config;

//首页
class Index extends App
{
    
	//填报志愿
    public function get_mobile_home_data()
    {
		$user_id = $this->user_id;
		
        $data = array('all_nums'=>'','chong_nums'=>'','wen_nums'=>'','bao_nums'=>'');		
		
		if(!empty($user_id)){
			if(cache('level_nums_'.$user_id)){
				$data = cache('level_nums_'.$user_id);
			}
		}
		$dataReturn = ['total_nums'=>$data['all_nums'],'chong_nums'=>$data['chong_nums'],'shi_nums'=>$data['wen_nums'],'wen_nums'=>$data['bao_nums']];
		
		return $this->show(1,"成功", $dataReturn);
	}
	
    
    public function home_data()
    {
		$map['status'] = 1;
		$map['groupid'] = 2;
		$student_nums = 1789078;//Db::name("member")->count();
		$zhiyuan_nums = 1789078;//Db::name("member_zhiyuan")->count();
		$zhuanjia_nums = 258;//Db::name("admin")->where($map)->count();
        
		$map['zj_is_home'] = 1;
		$zhuanjia_list = Db::name("admin")->where($map)->field('id,real_name,zj_job,portrait')->limit(100)->select();
		$zhuanjia_list = array_map(function ($rs){
			$rs['portrait'] = config('app_host').'/uploads/face/'.$rs['portrait'];
			$rs['zj_job'] = explode('|',$rs['zj_job']);
			
            return $rs;
        }, $zhuanjia_list);	
		
		
		$school_list = Db::name("school")->field('id,college_name,cover_image')->order('hot_nums desc,ranking asc')->limit(20)->select();	
		foreach($school_list as $k=>$v){
			$school_list[$k]['school_tese'] = Db::name('school_tese_data')->alias('d')->join('school_tese t','d.tese_id=t.id')->where('d.school_id',$v['id'])->column('t.title');
		}
		
		$article_list = Db::name("article")->where('is_tui',1)->field('id,title,remark,writer,views,photo,create_time')->order('id desc')->limit(3)->select(); 
		$article_list = array_map(function ($rs){
            $rs["create_time"] = !empty($rs["create_time"])?date('Y-m-d H:i',$rs["create_time"]):'';
			
			$photos = explode(',',$rs['photo']);
			$photos = array_filter($photos);
			foreach($photos as $kp=>$vp){ 
				$photos[$kp] = config('app_host').$vp;
			}
			$rs['photo'] = $photos;
			$rs['photo_count'] = count($photos);
			
            return $rs;
        }, $article_list);			
			
        $data = array('student_nums'=>$student_nums,'zhiyuan_nums'=>$zhiyuan_nums,'zhuanjia_nums'=>$zhuanjia_nums,'zhuanjia_list'=>$zhuanjia_list,'school_list'=>$school_list,'article_list'=>$article_list);
		return $this->show(1,"成功", $data);
		
    }
    
	
	
    public function course_data()
    {
        $subject_id = $this->request->get("subject_id",0,'intval');
		$map['status'] = 1;
		$map['is_home'] = 1;
		if(!empty($subject_id)){
			$map['subject_id'] = $subject_id;
		}
		$list = Db::name("course")->where($map)->field('id,title,study_nums,photo')->select();
		
		$list = array_map(function ($rs){
            $rs["less_nums"] = Db::name("course_less")->where('course_id',$rs['id'])->count();
			return $rs;
        }, $list);	
		
		return $this->show(1,"成功", $list);
		
    }
	
	
	public function get_ceping_cate()
    {
		$list = Db::name('question_cat')->field('id,name')->where('is_show',1)->order('sort asc,id asc')->select();
		return $this->show(1,"success", $list);
	}
	
	
    public function ceping_data()
    {
        $cate_id = $this->request->get("cate_id",0,'intval');
		if(!empty($cate_id)){
			$map['cat_id'] = $cate_id;
		}
		$map['is_show'] = 1;
		$map['is_home'] = 1;
		
		$list = Db::name("ceping")->where($map)->field('id,name,cover,algorithm')->select();
		$list = array_map(function ($rs){
			$rs['cover'] = config('app_host').$rs['cover'];
			$timu_list = service("ceping\\".$rs['algorithm'])->generateTest();
			$rs['question_nums'] = count($timu_list);
			return $rs;
        }, $list);	
		
		return $this->show(1,"成功", $list);
		
    }
    
}